﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Silverlight3dApp.Recipes.Chapter01.Lesson04
{
    public class CoordinateCross
    {
        private readonly GraphicsDevice _graphicsDevice;
        private readonly BasicEffect _basicEffect;

        private readonly VertexPositionColor[] _vertices;
        private readonly VertexBuffer _vertexBuffer;

        public CoordinateCross(GraphicsDevice graphicsDevice)
        {
            _graphicsDevice = graphicsDevice;
            _basicEffect = new BasicEffect(graphicsDevice);

            _vertices = new VertexPositionColor[30];
            InitializeVertices();
            _vertexBuffer = new VertexBuffer(_graphicsDevice, VertexPositionColor.VertexDeclaration, _vertices.Length, BufferUsage.None);
            _vertexBuffer.SetData(_vertices);
        }

        private void InitializeVertices()
        {
            _vertices[0] = new VertexPositionColor(new Vector3(0, 0, 0), Color.White);
            _vertices[1] = new VertexPositionColor(new Vector3(5, 0, 0), Color.White);
            _vertices[2] = new VertexPositionColor(new Vector3(5, 0, 0), Color.White);
            _vertices[3] = new VertexPositionColor(new Vector3(4.5f, 0.5f, 0), Color.White);
            _vertices[4] = new VertexPositionColor(new Vector3(5, 0, 0), Color.White);
            _vertices[5] = new VertexPositionColor(new Vector3(4.5f, -0.5f, 0), Color.White);

            _vertices[6] = new VertexPositionColor(new Vector3(0, 0, 0), Color.White);
            _vertices[7] = new VertexPositionColor(new Vector3(0, 5, 0), Color.White);
            _vertices[8] = new VertexPositionColor(new Vector3(0, 5, 0), Color.White);
            _vertices[9] = new VertexPositionColor(new Vector3(0.5f, 4.5f, 0), Color.White);
            _vertices[10] = new VertexPositionColor(new Vector3(0, 5, 0), Color.White);
            _vertices[11] = new VertexPositionColor(new Vector3(-0.5f, 4.5f, 0), Color.White);

            _vertices[12] = new VertexPositionColor(new Vector3(0, 0, 0), Color.White);
            _vertices[13] = new VertexPositionColor(new Vector3(0, 0, -5), Color.White);
            _vertices[14] = new VertexPositionColor(new Vector3(0, 0, -5), Color.White);
            _vertices[15] = new VertexPositionColor(new Vector3(0, 0.5f, -4.5f), Color.White);
            _vertices[16] = new VertexPositionColor(new Vector3(0, 0, -5), Color.White);
            _vertices[17] = new VertexPositionColor(new Vector3(0, -0.5f, -4.5f), Color.White);
        }

        public void Draw(Matrix world, Matrix view, Matrix projection)
        {
            _graphicsDevice.SetVertexBuffer(_vertexBuffer);
            _basicEffect.World = world;
            _basicEffect.View = view;
            _basicEffect.Projection = projection;
            _basicEffect.VertexColorEnabled = true;
            foreach (var pass in _basicEffect.CurrentTechnique.Passes)
            {
                pass.Apply();
                _graphicsDevice.DrawPrimitives(PrimitiveType.LineList, 0, 9);
            }
        }

        public void DrawUsingPresetEffect()
        {
            _graphicsDevice.SetVertexBuffer(_vertexBuffer);
            _graphicsDevice.DrawPrimitives(PrimitiveType.LineList, 0, 9);
        }
    }
}